/*
 * @(#)WebUtil.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.web;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;

/**
 * Common class provides some common methods for web tier.
 * 
 * @author lun.au
 */
public class WebUtil {
  
  public static final int MAX_LENGTH_OF_FILE_CONTEXT_MENU_TITLE = 45;
  public static final int MAX_LENGTH_OF_FOLDER_CONTEXT_MENU_TITLE = 20;

  public static SessionContainer getSessionContainer(ServletRequest req) {
    if (!Utility.isEmpty(((HttpServletRequest) req).getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY))) {
      return (SessionContainer) ((HttpServletRequest) req).getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    } else {
      return null;
    }
  }

  public static SessionContainer getSessionContainer(ServletRequest req, boolean createNew) {
    SessionContainer sessionContainer = getSessionContainer(req);
    if (Utility.isEmpty(sessionContainer) && createNew) {
      sessionContainer = new SessionContainer();
    }
    return sessionContainer;
  }

  public static String getContextPath(PageContext pageContext) {
    return (((HttpServletRequest) pageContext.getRequest()).getContextPath());
  }

  public static String getPreference(ServletRequest req) {
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    SessionContainer sessionContainer = getSessionContainer(req);
    if ((sessionContainer != null) && (sessionContainer.getUserRecord() != null)) {
      if (sessionContainer.getUserRecord().getPreference() != null) {
        preference = sessionContainer.getUserRecord().getPreference().toString();
      }
    }
    return preference;
  }

  public static String getSessionLocale(ServletRequest req) {
    String locale = SystemParameterFactory.getSystemParameter(SystemParameterConstant.LOCALE);
    SessionContainer sessionContainer = getSessionContainer(req);
    if ((sessionContainer != null) && (sessionContainer.getUserRecord() != null)) {
      if (sessionContainer.getUserRecord().getLocale() != null) {
        locale = sessionContainer.getUserRecord().getLocale().toString();
      }
    }
    return locale;
  }

  public static String getImage(ServletRequest req, String src) {
    return getImage(req, src, true);
  }

  public static String getImage(ServletRequest req, String src, boolean common) {
    String preference = getPreference(req);
    String locale = getSessionLocale(req);
    if (common) {
      return ((HttpServletRequest) req).getContextPath() + "/theme/pref" + preference + "/img/common/" + src;
    } else {
      return ((HttpServletRequest) req).getContextPath() + "/theme/pref" + preference + "/img/lang/" + locale + "/" + src;
    }
  }

  public static String maxString(String targetString, int maxLength) {
    if (Utility.isEmpty(targetString) || maxLength <= 0) {
      return targetString;
    }

    if (targetString.length() < maxLength) {
      return targetString;
    }

    StringBuffer buffer = new StringBuffer(maxLength + 3);
    buffer.append(targetString.substring(0, maxLength));
    buffer.append("...");

    return (buffer.toString());
  }
}
